#ifndef KVDB_CLIENT_KVDBCLIENT_H_
#define KVDB_CLIENT_KVDBCLIENT_H_

#include "kvdb/base/basedefines.h"
#include "vzconn/base/basicdefines.h"
#include "vzconn/base/bytebuffer.h"
#include "vzconn/conn/ipcconnect.h"

namespace kvdb {

class KvdbClient : public boost::noncopyable,
  public boost::enable_shared_from_this<KvdbClient> {
 public:
  typedef boost::shared_ptr<KvdbClient> Ptr;
  KvdbClient();
  virtual ~KvdbClient();
  // default addr 127.0.0.1
  // default port 5299
  bool InitKvdbClient(const char *server, uint16 port);
  void UinitKvdbClient();
  //
  bool SetKey(const char *key, uint8 key_size,
              const char *value, uint32 value_size);
  bool SetKey(const std::string key,
              const char *value, uint32 value_size);

  bool GetKey(const char *key, uint8 key_size,
              std::string *result,
              bool absolute = false);
  bool GetKey(const std::string key,
              std::string *result,
              bool absolute = false);
  bool GetKey(const std::string key,
              void *buffer,
              std::size_t buffer_size,
              bool absolute = false);
  bool GetKey(const char *key, uint8 key_size,
              void *buffer, std::size_t buffer_size,
              bool absolute = false);

  bool DeleteKey(const char *key, uint8 key_size);

 private:
  vzconn::ByteBuffer::Ptr PrepareBuffer(uint8 type,
                                        const char *key,
                                        uint8 key_size,
                                        const char *value,
                                        uint32 value_size);

  void ReplaceBufferKeyValue(const std::string key, const std::string value);
  void DeleteBufferKeyValue(const std::string key);

  bool ParseGetKey(const char *data, int size, std::string *result);
  bool CheckConnected();
 private:
  boost::asio::io_service io_service_;
  vzconn::BlockVzConnect::Ptr block_connect_;
  //
  std::vector<char> read_buffer_;
  vzconn::ByteBuffer::Ptr write_buffer_;
  std::map<std::string, std::string> key_values_;
  std::string server_addr_;
  uint16 server_port_;
};
}

#endif // KVDB_CLIENT_KVDBCLIENT_H_